//
//  ViewController.swift
//  C016_Quartz2D
//
//  Created by yao_yu on 15/6/22.
//  Copyright © 2015年 yao_yu. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    let myView = MyView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        view.addSubview(myView)
        myView.backgroundColor = UIColor.clearColor()
        myView.frame = CGRectMake(20, 100, 300, 300)
    }
    
    override func viewDidLayoutSubviews() {
       
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

class MyView: UIView {
    override func drawRect(rect: CGRect) {
        //draw2()
        drawBarChart(rect)
    }
    
    func draw2() {
        let context:CGContext = UIGraphicsGetCurrentContext()
        let path:CGMutablePath = CGPathCreateMutable()
        
        // 绘制一条线
        CGPathMoveToPoint(path, nil, 50, 50)
        CGPathAddLineToPoint(path, nil, 200, 200)
        // 绘制矩形
        CGPathAddRect(path, nil, CGRectMake(60, 60, 100, 100))
        CGPathAddEllipseInRect(path, nil, CGRectMake(60, 60, 100, 100))
        // 圆角矩形
        CGPathAddRoundedRect(path, nil, CGRectMake(60, 60, 100, 100), 15, 15)
        
        CGPathAddArc(path, nil, 100, 300, 30, 0, CGFloat( M_PI_2), true)
        CGContextAddPath(context, path)
        CGContextStrokePath(context)
    }
    
    func draw3() {
        let path:UIBezierPath = UIBezierPath()
        path.addArcWithCenter(CGPointMake(100, 100), radius: 60, startAngle: 0, endAngle: CGFloat(M_PI_2), clockwise: true)
        path.addLineToPoint(CGPointMake(100, 100))
        path.addLineToPoint(CGPointMake(300, 100))
        path.addLineToPoint(CGPointMake(100, 300))
        UIColor.redColor().setStroke()
        path.lineWidth = 5
        path.lineCapStyle = kCGLineCapRound
        path.lineJoinStyle = kCGLineJoinRound
        
        path.moveToPoint(CGPointMake(150, 50))
        path.addLineToPoint(CGPointMake(250, 90))
        path.addLineToPoint(CGPointMake(210, 190))
        path.addLineToPoint(CGPointMake(90, 190))
        path.addLineToPoint(CGPointMake(50, 90))
        
        path.fill()
        path.stroke()
    }
    
    func draw4() {
        // Drawing code
        // 获得图形上下文
        let ct = UIGraphicsGetCurrentContext();
        // 绘制路径
        CGContextMoveToPoint(ct, 100, 100);
        
        CGContextAddLineToPoint(ct, 200, 200);
        
        CGContextAddLineToPoint(ct, 100, 300);
        
        // 设置绘图状态，一定要在渲染之前设置，并且一经设置，状态会一直持续下去，除非再次改变。
        CGContextSetLineWidth(ct, 5);
        UIColor.redColor().setStroke()
        CGContextSetLineJoin(ct, kCGLineJoinRound);
        CGContextSetLineCap(ct, kCGLineCapRound);
        // 渲染
        CGContextStrokePath(ct);
    }
    
    func draw5() {
        // Drawing code
        // 绘制多个状态不同的线
        // 获得图形上下文
        let path = UIBezierPath();
        // 绘制路径
        path.moveToPoint(CGPointMake(100, 100));
        path.addLineToPoint(CGPointMake(200, 200))
        // 设置绘图状态，一定要在渲染之前设置，并且一经设置，状态会一直持续下去，除非再次改变。
        UIColor.redColor().setStroke()
        // 渲染
        path.stroke()
        
        // 获得图形上下文
        let path1 = UIBezierPath();
        // 绘制路径
        path1.moveToPoint(CGPointMake(200 , 200))
        path1.addLineToPoint(CGPointMake(100, 150))
        // 设置绘图状态，一定要在渲染之前设置，并且一经设置，状态会一直持续下去，除非再次改变。
        UIColor.blueColor().setStroke()
        // 渲染
        path1.stroke()
    }
    
    func drawPieChart() {
        let arr = randomArray // 随机地返回一个数组，数组元素和为100
        // 获取半径和圆心
        let radius = self.frame.size.height/2 - 2;
        let center = CGPointMake(radius, radius);
        // 绘制角度
        var startAngle:CGFloat = 0;
        var endAngle:CGFloat = 0;
        
        // 绘制图形
        for percent in arr {
            
            // 计算角度
            endAngle = startAngle + percent / 100.0 * CGFloat( M_PI) * 2;
            // 绘制图形
            let path = UIBezierPath(arcCenter: center, radius:radius, startAngle:startAngle, endAngle:endAngle, clockwise:true)
            path.addLineToPoint(center)
            
            startAngle = endAngle;
            // 设置颜色
            randomColor().set()

            // 填充，并且把终点和起始点连接起来
            path.fill()
        }
    }
    
    func drawBarChart(rect: CGRect) {
        let arr = randomArray
        var x:CGFloat = 0
        var y:CGFloat = 0
        var w:CGFloat = 0
        var h:CGFloat = 0
        
        let count:CGFloat = CGFloat( arr.count)
        w = rect.size.width / (2 * count - 1)
        for (i, percent) in arr.enumerate() {
            x = 2 * CGFloat(i) * w
            h = percent / 100.0 * rect.size.height
            
            y = rect.size.height - h
            let path = UIBezierPath(rect: CGRectMake(x, y, w, h))
            path.lineWidth = 1
            randomColor().set()
            UIColor.redColor().setStroke()
            path.fill()
            path.stroke()
        }
    }
    
    var randomArray: [CGFloat] {
        var arr = [CGFloat]()
        var total:UInt32 = 100
        var temp:UInt32 = 0
        for (var i:UInt32 = 0; i < arc4random_uniform(10) + 1; i++){
            temp = arc4random_uniform(total) + 1
            arr.append(CGFloat( temp))
            if (temp == total)
            {
                break
            }
            total -= temp
        }
        if (total > 0){
            arr.append(CGFloat( total))
        }
        return arr
    }
    
    func randomColor() -> UIColor {
        let r = CGFloat( arc4random_uniform(256)) / 255.0
        let g = CGFloat( arc4random_uniform(256)) / 255.0
        let b = CGFloat( arc4random_uniform(256)) / 255.0
        return UIColor(red: r, green: g, blue: b, alpha: 1)
    }
}

